<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>canvas Image</title>
  <style>
    body{
      text-align: center;
    }
    #canvas{
      background-color: #cccccc;
    }
  </style>
</head>
<body>
  <h3>演示 canvas图片</h3>
  <canvas id="canvas" width="500" height="400"></canvas>

  <script>
    var canvas = document.getElementById("canvas");
    var ctx = canvas.getContext("2d");

    var img = new Image();  // 创建图片对象
    img.src = "../res/p3.png"; // 加载图片
    img.onload = function () {
      // 加载成功回调 

      // 左上角画一张
      ctx.drawImage(img, 0, 0);

      // 获得图片宽高
      var w = img.width;
      var h = img.height;
      // 左上角画一张
      ctx.drawImage(img, 500-w,0);

      // 左下角画一张 指定图片宽高
      ctx.drawImage(img, 0, 400-50, 100, 50);
    }

    

  </script>
</body>
</html>